
package com.jf.cloud.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.jf.cloud.api.distribution.feign.DistributionFeignClient;
import com.jf.cloud.api.group.feign.GroupFeignClient;
import com.jf.cloud.api.seckill.feign.SeckillFeignClient;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.constant.SpuType;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.product.constant.SpuStatus;
import com.jf.cloud.product.mapper.BrandMapper;
import com.jf.cloud.product.mapper.SpuMapper;
import com.jf.cloud.product.service.SpuOfflineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * spu信息
 *
 * @author zz
 * @date 2021-07-03 15:27:24
 */
@Service
public class SpuOfflineServiceImpl implements SpuOfflineService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private GroupFeignClient groupFeignClient;
    @Autowired
    private SeckillFeignClient seckillFeignClient;
    @Autowired
    private DistributionFeignClient distributionFeignClient;

    @Override
    public void offlineSpuStatusAndActivity(Integer type, List<Long> spuIds, Long shopId, List<Long> categoryIds, Integer status, List<Long> shopIds) {
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(type,spuIds,shopId,categoryIds,status,shopIds);
        if(CollectionUtil.isEmpty(spuList)){
            return;
        }
        List<Long> seckillSpuIds = new ArrayList<>();
        List<Long> groupSpuIds = new ArrayList<>();
        for (SpuVO spuVO : spuList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())){
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())){
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }

            // 商品那边可能有平台下架和商家下架，其余都为0，商家下架
            if(type != 1){
                spuVO.setStatus(0);
            }
        }
        spuMapper.updateBatch(spuList);
        // 下线秒杀、团购活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // 更新品牌商品数量
        spuIds = spuList.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(spuIds)) {
            List<Long> brandIds = brandMapper.listBrandIdBySpuIds(spuIds);
            if (CollUtil.isNotEmpty(brandIds)) {
                brandMapper.batchUpdateSpuCount(brandIds);
            }
        }
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds,new ArrayList<>(), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }

    @Override
    public void offlineSpuStatusAndActivityByBrandId(List<Long> spuIds, Long brandId) {
        Integer sysType = AuthUserContext.get().getSysType();
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(1, spuIds,null,null,null,null);
        List<Long> seckillSpuIds = new ArrayList<>();
        List<Long> groupSpuIds = new ArrayList<>();
        for (SpuVO spuVO : spuList) {
            spuVO.setBrandId(null);
            spuVO.setStatus(SpuStatus.OFF_SHELF.value());
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())){
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())){
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
        }
        if(CollectionUtil.isEmpty(spuList)){
            return;
        }
        spuMapper.updateBatchFieldSetNull(spuList);
        // 下线对应的活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // dev
        if (Objects.nonNull(brandId) && !Objects.equals(brandId, Constant.ZERO_LONG)) {
            brandMapper.batchUpdateSpuCount(Collections.singleton(brandId));
        }
//        // 更新品牌商品数量
//        brandService.updateSpuCount(brandId, sysType);
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, new ArrayList<>(), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }


    private void offlineActivityBySpuIds(List<Long> seckillSpuIds, List<Long> groupSpuIds) {
        List<Long> spuIds = new ArrayList<>();
        // 下线对应的活动
        if (CollectionUtil.isNotEmpty(seckillSpuIds)) {
            spuIds.addAll(seckillSpuIds);
            ServerResponseEntity<Void> responseEntity = seckillFeignClient.offlineSeckillBySpuIds(seckillSpuIds);
            if (!responseEntity.isSuccess()) {
                throw new LuckException(responseEntity.getMsg());
            }
        }
        // 下线对应的团购活动
        if (CollectionUtil.isNotEmpty(groupSpuIds)) {
            spuIds.addAll(groupSpuIds);
            ServerResponseEntity<Void> responseEntity = groupFeignClient.offlineGroupBySpuIds(groupSpuIds);
            if (!responseEntity.isSuccess()) {
                throw new LuckException(responseEntity.getMsg());
            }
        }

    }


    @Override
    public void offlineCommissionSpuStatusAndActivity(Integer type, List<Long> spuIds, Long shopId, List<Long> categoryIds, Integer status,Integer sysType) {
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(type,spuIds,shopId,categoryIds,status,  null);
        List<Long> seckillSpuIds = new ArrayList<>();
        List<Long> groupSpuIds = new ArrayList<>();
        for (SpuVO spuVO : spuList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())){
                spuVO.setSpuType(SpuType.NORMAL.value());
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())){
                spuVO.setSpuType(SpuType.NORMAL.value());
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }
            // 商品那边可能有平台下架和商家下架，其余都为0，商家下架
            if(type != 1){
                spuVO.setStatus(0);
            }
        }
        if(CollectionUtil.isEmpty(spuList)){
            return;
        }
        spuMapper.updateBatch(spuList);
        // 下线秒杀、团购活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // 更新品牌商品数量
        spuIds = spuList.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(spuIds)) {
            List<Long> brandIds = brandMapper.listBrandIdBySpuIds(spuIds);
            if (CollUtil.isNotEmpty(brandIds)) {
                brandMapper.batchUpdateSpuCount(brandIds);
            }
        }
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, Collections.singletonList(shopId), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }
}
